package fab.forms.fields 
{
	import flash.text.TextField;
	import fab.forms.error.FormError;
	import fab.forms.fields.Element;
	/**
	 * ...
	 * @author fab
	 */
	public class InputTextElement extends Element
	{
		private var _tf:TextField;
		
		public function InputTextElement(tf:TextField, na:String=null) 
		{
			_tf = tf;
			//_tf.text = "";
			//_tf.multiline = false;
			name = na?na:tf.name;
		}
		override public function getValue():String
		{
			return _tf.text;
		}			
		override public function validate():FormError
		{
			var error:FormError;
			//trace(hasError( FormError.NONEMPTY ) ,"n", !validateNonempty());
			if ( hasError( FormError.NONEMPTY ) && !validateNonempty() )
			{
				error = getError( FormError.NONEMPTY );
			}
			else  if ( hasError( FormError.EMAIL ) && !validateEmail() )
			{
				error = getError( FormError.EMAIL );
			}
			else  if ( hasError( FormError.PHONENUMBER ) && !validatePhoneNumber() )
			{
				error = getError( FormError.PHONENUMBER );
			}			
			return error;
		}	
		
		
		private function validateNonempty():Boolean
		{
			return  _tf.text != "";
		}
        private function validateEmail():Boolean {
			
			var str:String = _tf.text;
			 
            var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
            var result:Object = pattern.exec(str);
            if(result == null) {
                return false;
            }
            return true;
        }
        private function validatePhoneNumber():Boolean {
			
			var str:String = _tf.text;
			
            var pattern:RegExp = /^\d{3}-\d{3}-\d{4}$/;
            var result:Object = pattern.exec(str);
            if(result == null) {
                return false;
            }
            return true;
        }		
	}

}